package face.pay.task.order.message;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import face.pay.job.config.rocket.MqConfig;
import face.pay.constant.OemType;
import face.pay.dao.CpnCardDetailInfoDao;
import face.pay.entity.CpnCardDetailInfo;
import face.pay.entity.OrderInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MessageSender {

    @Autowired
    MqConfig mqConfig;

    @Autowired
    ProducerBean producerBean;

    @Autowired
    CpnCardDetailInfoDao cpnCardDetailInfoDao;


    @Async
    public void sendMessage(OrderInfo orderInfo, String type){

        try{
            Thread.sleep(100);
        }catch (Exception e){
            e.printStackTrace();
        }

        String  json =  JSON.toJSONString(orderInfo);

        String topic = mqConfig.getOrderTopic();

        String tag = mqConfig.getProfitTag();

        log.info("oem服务商类型"+type);

        if(OemType.OPEN.equals(type)){

            topic = mqConfig.getCallBackOrderTopic();
            tag = mqConfig.getBackTag();
        }

        Message message = new Message(topic, tag, json.getBytes());

        try {
            SendResult sendResult = producerBean.send(message);
            if(sendResult!=null){

                String message_id =    sendResult.getMessageId();
                log.info("发送分消息成功 messageId "+message_id);
            }
        } catch (Exception e) {
            log.error("发送消息失败",e);
        }
    }

    @Async
    public void sendSharingMessage(OrderInfo orderInfo, String type){

        try{
            Thread.sleep(100);
        }catch (Exception e){
            e.printStackTrace();
        }

        String  json =  JSON.toJSONString(orderInfo);

        String topic = mqConfig.getSharingTopic();

        String tag = mqConfig.getSharingTag();

        log.info("oem服务商类型"+type);

        Message message = new Message(topic, tag, json.getBytes());

        try {
            SendResult sendResult = producerBean.send(message);
            if(sendResult!=null){

                String message_id =    sendResult.getMessageId();
                log.info("发送分消息成功 messageId "+message_id);
            }
        } catch (Exception e) {
            log.error("发送消息失败",e);
        }
    }
}
